package com.spring_book.spring_book.config;

import com.spring_book.spring_book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Object handler(Exception e) {
      log.error("发生异常 e:{}",e);
        return Result.fail();
    }
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public Object handler(ArrayIndexOutOfBoundsException e) {
        log.error("发生异常 e:{}",e);
        return Result.fail("发生数组越界异常");
    }
    @ExceptionHandler
    public Object handler(NullPointerException e) {
        log.error("发生异常 e:{}",e);
        return Result.fail("发生空指针异常");
    }
}
